﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Secure;
using Secure.Common;

namespace Secure
{
    public class GatewayService : ISecureGateway
    {
        private readonly List<SecuredResource> _securedResources = new List<SecuredResource>();

        public void AddSecuredResource(SecuredResource resource)
        {
            _securedResources.Add(resource);
        }

        public bool IsAllowedAccess(ISecureUser user, string resourcePath)
        {
            SecuredResource resource = GetResourceByPath(resourcePath);

            if (resource == null)
            {
                //log attempt to non-existent resource
                return false;
            }

            bool isAllowed = resource.IsAllowedAccess(user);

            if (!isAllowed)
            {
                // log access failure
            }

            return isAllowed;
        }

        private SecuredResource GetResourceByPath(string resourcePath)
        {
            return _securedResources.Find((r) => r.GetResourcePath() == resourcePath);
        }
    }
}